﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eService.BLL;
using HLVControl.Grid.Data;
using APP.QUANLY.Common;

namespace eService.Presentation
{
    public partial class usrcDanhMuc_KhachHang : UserControl
    {
       EnumNameForm.ThaoTac thaotac = EnumNameForm.ThaoTac.macdinh;
        ExcuteKHACHHANG exe;

        public usrcDanhMuc_KhachHang()
        {
            InitializeComponent();
        }

        private void usrcDanhMuc_KhachHang_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        protected virtual void LoadAll()
        {
            LoadConnection();
            LoadUI();
        }
        private void LoadConnection()
        {
            ConnectionProvider.LoadAllConnection();
        }
        private void LoadUI()
        {
            exe = new ExcuteKHACHHANG();
            thaotac = EnumNameForm.ThaoTac.macdinh;
            ControlUI(thaotac);
            UIHelper.LoadFocus(txtTimKiem);
        }
        private void ControlUI(EnumNameForm.ThaoTac thaotac)
        {
            if (thaotac == EnumNameForm.ThaoTac.them || thaotac == EnumNameForm.ThaoTac.sua)
            {
                SetStateButton(false);
                if (thaotac == EnumNameForm.ThaoTac.them)
                    LoadValuesToUI(null);
                SetTextReadOnly(false);
                UIHelper.LoadFocus(txtTen);
            }
            if (thaotac == EnumNameForm.ThaoTac.macdinh || thaotac == EnumNameForm.ThaoTac.xoa)
            {
                SetStateButton(true);
                LoadValuesToUI(null);
                SetTextReadOnly(true);

                //Load TreeView
                FillTreeView();
                treeview.RefreshGrouping();

                txtTimKiem.Text = "";
                UIHelper.LoadFocus(txtTimKiem);
            }
        }
        private void SetStateButton(bool IsEnabled)
        {
            btnSua.Enabled = IsEnabled;
            btnThem.Enabled = IsEnabled;
            btnXoa.Enabled = IsEnabled;
            btnXacNhan.Visible = !IsEnabled;
            btnHuy.Visible = !IsEnabled;
            treeview.Enabled = IsEnabled;
            btnTimKiem.Enabled = IsEnabled;
        }
        private void SetTextReadOnly(bool IsReadOnly)
        {
            txtMa.ReadOnly = IsReadOnly;
            txtTen.ReadOnly = IsReadOnly;
            txtDiaChi.ReadOnly = IsReadOnly;
            txtDienThoai.ReadOnly = IsReadOnly;
            txtFax.ReadOnly = IsReadOnly;
            txtEmail.ReadOnly = IsReadOnly;
            numKMGio.Enabled = !IsReadOnly;
            numKMTien.Enabled = !IsReadOnly;
            chbKhachVIP.Enabled = !IsReadOnly;
        }
        private void LoadValuesToUI(PARTNERINFO obj)
        {
            if (obj == null)
            {
                obj = new PARTNERINFO();
                obj.PromoteTime = 0;
                obj.PromoteAll = 0;
                obj.IsVIP = false;
            }
            txtMa.Text = obj.PartnerID.ToString();
            txtTen.Text = obj.Name;
            txtDienThoai.Text = obj.Phone;
            txtFax.Text = obj.Fax;
            txtDiaChi.Text = obj.Address;
            txtEmail.Text = obj.Email;
            numKMGio.Value = (Decimal)obj.PromoteTime;
            numKMTien.Value = (Decimal)obj.PromoteAll;
            chbKhachVIP.Checked = (bool)obj.IsVIP;
        }
        private void FillTreeView()
        {
            try
            {
                List<PARTNERINFO> data = exe.GetAll();
                exe.LoadToTreeview(treeview, data);
            }
            catch { }
        }
        private bool XuLy(EnumNameForm.ThaoTac thaotac)
        {
            var flag = false;
            try
            {
                PARTNERINFO obj = exe.LoadValues(int.Parse(txtMa.Text), txtTen.Text, txtDiaChi.Text, txtDienThoai.Text, txtFax.Text, txtEmail.Text, (double)numKMGio.Value,(double)numKMTien.Value,chbKhachVIP.Checked);
                flag = exe.KQ;
                if (flag)
                {
                    switch (thaotac)
                    {
                        case EnumNameForm.ThaoTac.them:
                            exe.Add(obj);
                            break;
                        case EnumNameForm.ThaoTac.sua:
                            exe.UpDate(obj);
                            break;
                        case EnumNameForm.ThaoTac.xoa:
                            exe.Delete(obj);
                            break;
                    }
                    flag = exe.KQ;
                }
                MessageBox.Show(exe.ErrMsg, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        private void button_Click(object sender, EventArgs e)
        {
            bool KQ = false;
            Button btn = (Button)sender;
            string _thaotac = btn.Tag.ToString();
            if ((_thaotac == "sua" && txtMa.Text == "0") || (_thaotac == "xoa" && txtMa.Text == "0"))
            {
                MessageBox.Show("Vui lòng chọn khách hàng!");
                return;
            }
            switch (_thaotac)
            {
                case "them":
                    thaotac = EnumNameForm.ThaoTac.them;
                    break;
                case "sua":
                    thaotac = EnumNameForm.ThaoTac.sua;
                    break;
                case "huy":
                    thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xoa":
                    KQ = XuLy(EnumNameForm.ThaoTac.xoa);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
                case "xacnhan":
                    KQ = XuLy(thaotac);
                    if (KQ) thaotac = EnumNameForm.ThaoTac.macdinh;
                    break;
            }
            ControlUI(thaotac);
        }
        private void treeview_AfterSelectionChange(object sender, HLVControl.Grid.Events.TreeListElementEventArgs e)
        {
            try
            {
                TreeListRow firstSelected = treeview.SelectedElements[0] as TreeListRow;
                int ID = int.Parse(firstSelected.Cells["colID"].Value.ToString());
                PARTNERINFO obj = exe.GetByID(ID);
                if (obj == null) return;
                LoadValuesToUI(obj);
            }
            catch
            {

            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            SearchProvider.SmartSearch(treeview, txtTimKiem.Text.Trim(), new List<string> { "10", "11", "12", "13", "14", "15", "16" });
        }
        private void txt_Enter(object sender, EventArgs e)
        {
            UIHelper.txt_Enter(sender, e);
        }
        private void txt_Leave(object sender, EventArgs e)
        {
            UIHelper.txt_Leave(sender, e);
        }
        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (sender is TextBox)
            {
                TextBox txt = sender as TextBox;
                if (txt.Name == txtTen.Name)
                    UIHelper.txt_KeyPress(sender, txtDienThoai, e);
                if (txt.Name == txtDienThoai.Name)
                    UIHelper.txt_KeyPress(sender, txtFax, e);
                if (txt.Name == txtFax.Name)
                    UIHelper.txt_KeyPress(sender, txtEmail, e);
                if (txt.Name == txtEmail.Name)
                    UIHelper.txt_KeyPress(sender, txtDiaChi, e);
                if (txt.Name == txtDiaChi.Name)
                    UIHelper.txt_KeyPress(sender, btnXacNhan, e);
                if (txt.Name == txtTimKiem.Name)
                    UIHelper.txt_KeyPress(sender, btnTimKiem, e);
            }
        }
    }
}
